//
//  LVSessionListViewController.m
//  LEVE
//
//  Created by 上官惠阳 on 2017/3/10.
//  Copyright © 2017年 dashuju. All rights reserved.
//

#import "LVSessionListViewController.h"
#import "HYCustomCommonEmptyView.h"
#import "LVSessionListCell.h"
#import "LVSessionModel.h"
#import "LVSSessionSearchView.h"
#import "PYSearchResultModel.h"
#import "PYSearchViewController.h"
#import "PYSearchResultCell.h"
#import "LVSessionQuickPopUpView.h"
#import "LVSessionMorePopView.h"
#import "LVCustomAlertView.h"
#import "LVSessionEditNamePopView.h"
#import "LVSessionReportReasonPopView.h"
//#import "LVSessionViewController.h"
#import "GJGCChatFriendViewController.h"

@interface LVSessionListViewController ()<PYSearchViewControllerDelegate,PYSearchViewControllerDataSource>

@property (strong, nonatomic)HYCustomCommonEmptyView *emptyView;
@property (strong,nonatomic)NSMutableArray *searchSuggestionsM;//搜索结果
@end

@implementation LVSessionListViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    self.refreshType = HYBaseTableVcRefreshTypeOnlyCanRefresh;

    [self loadData];

    self.needCellSepLine = NO;

    [self createTableHeader];
}
- (void)loadData
{
    for (int i = 0; i < 12; i++) {
        LVSessionModel *model = [[LVSessionModel alloc] init];
        if (i == 2) {
            model.unReadCount = @"8";
        }
        [self.dataArray addObject:model];
    }
    if (!self.dataArray.count) {
        [self.emptyView showInView:self.view];
    }else{
        [self.emptyView removeFromSuperview];
    }

    [self HY_reloadData];
}
#pragma mark refresh
- (void)HY_refresh
{
    [super HY_refresh];

    [self.dataArray removeAllObjects];
    [self loadData];

    [self HY_endRefresh];
}
#pragma mark showPopView
- (void)showSessionQuickPopViewWithModel:(LVSessionModel *)model
{
    WeakSelf(weakSelf);
    LVSessionQuickAction *rightAction = [[LVSessionQuickAction alloc] initWithImageName:@"im_popup_up" action:^{

    }];
    LVSessionQuickAction *imAction = [[LVSessionQuickAction alloc] initWithImageName:@"im_popup_dialogue" action:^{

    }];
    LVSessionQuickAction *cameraAction = [[LVSessionQuickAction alloc] initWithImageName:@"im_popup_camera" action:^{

    }];
    LVSessionQuickAction *shareAction = [[LVSessionQuickAction alloc] initWithImageName:@"im_popup_share" action:^{

    }];
    LVSessionQuickAction *moreAction = [[LVSessionQuickAction alloc] initWithImageName:@"im_popup_more" action:^{
        [weakSelf showMorePopViewWithModel:model];
    }];
    NSArray *arr = @[imAction,cameraAction,shareAction,moreAction];
    LVSessionQuickPopUpView *popView = [[LVSessionQuickPopUpView alloc] initWithSessionModel:model rightAction:rightAction actions:arr];
    [popView showInView:self.view.window];
}
- (void)showMorePopViewWithModel:(LVSessionModel *)model
{
    WeakSelf(weakSelf);
    LVSessionMorePopView *morePopView = [LVSessionMorePopView actionMorePopViewWithCancelTitle:@"取消" alertTitle:@"William" SubTitle:@"silly man" btnTitles:@"清除列表",@"编辑名字",@"加入黑名单",@"举报",@"删除", nil];
    [morePopView showInView:self.view.window];
    [morePopView setCustomActionPopViewItemClickHandle:^(LVSessionMorePopView *popView, NSInteger currentIndex, NSString *title) {
        switch (currentIndex) {
            case 0:
                //清除列表
                [weakSelf showDeleteSessionListAlertView];
                break;
            case 1:
                //编辑名字
                [weakSelf showEditNamePopViewWithModel:model];
                break;
            case 2:
                //加入黑名单
                [weakSelf showPullIntoBlackListAlertViewWithModel:model];
                break;
            case 3:
                //举报
                [weakSelf showReportReasonPopViewWithModel:model];
                break;
            case 4:
                //删除
                [weakSelf showDeleteFrieldAlertViewWithModel:model];
                break;
            default:
                break;
        }
    }];
}
- (void)showDeleteSessionListAlertView
{
    LVCustomAlertView *alertView = [[LVCustomAlertView alloc] initWithTitle:@"你确定要删除所有的会话列表吗？" cancel:@"取消" sure:@"确定"];
    [alertView showInView:self.view.window];
    [alertView setupSureBlock:^BOOL{

        return YES;
    }];
    [alertView setupCancelBlock:^BOOL{

        return YES;
    }];
}
- (void)showEditNamePopViewWithModel:(LVSessionModel *)model
{
    LVSessionEditNamePopView *editNamePopView = [[LVSessionEditNamePopView alloc] initWithTitle:@"William" cancel:@"取消" sure:@"确定"];
    [editNamePopView showInView:self.view.window];

    [editNamePopView setupSureBlock:^BOOL(NSString *str) {

        return YES;
    }];
    [editNamePopView setupCancelBlock:^BOOL{

        return YES;
    }];
}
- (void)showPullIntoBlackListAlertViewWithModel:(LVSessionModel *)model
{
    LVCustomAlertView *alertView = [[LVCustomAlertView alloc] initWithTitle:@"你确定要将此好友加入黑名单吗？" cancel:@"取消" sure:@"确定"];
    [alertView showInView:self.view.window];
    [alertView setupSureBlock:^BOOL{

        return YES;
    }];
    [alertView setupCancelBlock:^BOOL{

        return YES;
    }];
}
- (void)showDeleteFrieldAlertViewWithModel:(LVSessionModel *)model
{
    LVCustomAlertView *alertView = [[LVCustomAlertView alloc] initWithTitle:@"你确定要删除好友William吗？" cancel:@"取消" sure:@"确定"];
    [alertView showInView:self.view.window];
    [alertView setupSureBlock:^BOOL{

        return YES;
    }];
    [alertView setupCancelBlock:^BOOL{

        return YES;
    }];
}
- (void)showReportReasonPopViewWithModel:(LVSessionModel *)model
{
    LVSessionReportReasonPopView *reportPopView = [[LVSessionReportReasonPopView alloc] initWithTitle:@"举报原因" cancel:@"取消" sure:@"确定" reportReasons:@"广告和侮辱",@"色情",@"涉嫌欺诈信息传播",@"行情非法信息",@"其他负面的信息", nil];
    [reportPopView showInView:self.view.window];
    [reportPopView setupSureBlock:^BOOL(NSString *str) {

        return YES;
    }];
    [reportPopView setupCancelBlock:^BOOL{

        return YES;
    }];
}
#pragma mark - UITableViewDelegate
- (NSInteger)HY_numberOfSections {
    return 1;
}

- (NSInteger)HY_numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (HYBaseTableViewCell *)HY_cellAtIndexPath:(NSIndexPath *)indexPath {
    LVSessionModel *model = self.dataArray[indexPath.row];
    
    LVSessionListCell *cell = [LVSessionListCell nibCellWithTableView:self.tableView];
    cell.lastNewsType = indexPath.row % 6;
    WeakSelf(weakSelf);
    [cell addLongPressWithBlock:^(LVSessionModel *model) {
        [weakSelf showSessionQuickPopViewWithModel:model];
    }];
    [cell loadCellWithModel:model];
    return cell;
}
- (CGFloat)HY_cellheightAtIndexPath:(NSIndexPath *)indexPath {
    return 68;
}
- (void)HY_didSelectCellAtIndexPath:(NSIndexPath *)indexPath cell:(HYBaseTableViewCell *)cell
{
//    LVSessionViewController *sessionVc = [[LVSessionViewController alloc] init];
    
    GJGCChatFriendTalkModel *talk = [[GJGCChatFriendTalkModel alloc]init];
    talk.talkType = GJGCChatFriendTalkTypePrivate;
    talk.toId = @"10";
    talk.toUserName = @"Binson";

    GJGCChatFriendViewController *sessionVc = [[GJGCChatFriendViewController alloc]initWithTalkInfo:talk];
    [self pushVc:sessionVc];
}
#pragma mark emptyView
- (HYCustomCommonEmptyView *)emptyView {
    if (!_emptyView) {
        HYCustomCommonEmptyView *empty = [[HYCustomCommonEmptyView alloc] initWithTitle:@"暂无动态" secondTitle:@"" iconname:@"无动态"];
        [self.view addSubview:empty];
        _emptyView = empty;
    }
    return _emptyView;
}
#pragma mark createTableHeader
- (void)createTableHeader
{
    CGFloat searchHeigth = 52;

    LVSSessionSearchView *searchView = [[LVSSessionSearchView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, searchHeigth)];

    __weak typeof(self) weakSelf = self;
    searchView.searchBlock = ^(id obj){
        // 1.创建热门搜索
        NSArray *hotSeaches = @[@"傻啊", @"二吧", @"马蛋啊"];
        // 2. 创建控制器
        PYSearchViewController *searchViewController = [PYSearchViewController searchViewControllerWithHotSearches:hotSeaches searchBarPlaceholder:@"搜索" didSearchBlock:^(PYSearchViewController *searchViewController, UITextField *searchBar, NSString *searchText) {

        }];

        // 3.设置风格
        searchViewController.hotSearchStyle = PYHotSearchStyleColorfulTag; // 热门搜索风格为默认
        searchViewController.searchHistoryStyle = PYSearchHistoryStyleCell; // 搜索历史风格根据选择

        // 4. 设置代理
        searchViewController.delegate = weakSelf;
        searchViewController.dataSource = weakSelf;

        // 5. 跳转到搜索控制器
        [weakSelf.navigationController pushViewController:searchViewController animated:YES];
    };

    self.tableView.tableHeaderView = searchView;

    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 70)];
    self.tableView.tableFooterView = footerView;
}

#pragma mark - PYSearchViewControllerDelegate
- (void)searchViewController:(PYSearchViewController *)searchViewController searchTextDidChange:(UISearchBar *)seachBar searchText:(NSString *)searchText
{
    if (searchText.length) { // 与搜索条件再搜索

        [self searchViewController:searchViewController searchText:searchText];
    }
}
- (void)searchViewController:(PYSearchViewController *)searchViewController didSelectSearchSuggestionAtIndex:(NSInteger)index searchText:(NSString *)searchText
{

}
- (void)searchViewController:(PYSearchViewController *)searchViewController didSearchWithsearchBar:(UISearchBar *)searchBar searchText:(NSString *)searchText
{
    if (searchText.length) { // 与搜索条件再搜索
        [self searchViewController:searchViewController searchText:searchText];
    }
}
- (void)didClickCancel:(PYSearchViewController *)searchViewController
{
    [searchViewController.navigationController popViewControllerAnimated:NO];
}
#pragma mark PYSearchViewControllerDataSource
- (UITableViewCell *)searchSuggestionView:(UITableView *)SearchSuggestionView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PYSearchResultCell *cell = [SearchSuggestionView dequeueReusableCellWithIdentifier:@"PYSearchResultCell"];
    if (!cell) {
        [SearchSuggestionView registerNib:[UINib nibWithNibName:@"PYSearchResultCell" bundle:nil] forCellReuseIdentifier:@"PYSearchResultCell"];
        cell = [SearchSuggestionView dequeueReusableCellWithIdentifier:@"PYSearchResultCell"];
    }

    PYSearchResultModel *model = _searchSuggestionsM[indexPath.row];
    __weak typeof(self) weakSelf = self;
    cell.tapHeadBlock = ^(id obj){

    };

    [cell loadCellWithModel:model];
    return cell;

}
- (CGFloat)searchSuggestionView:(UITableView *)searchSuggestionView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 68.f;
}
- (NSInteger)searchSuggestionView:(UITableView *)searchSuggestionView numberOfRowsInSection:(NSInteger)section
{
    return _searchSuggestionsM.count;
}
#pragma mark private
- (void)searchViewController:(PYSearchViewController *)searchViewController
                  searchText:(NSString *)searchText
{
    __weak typeof(self) weakSelf = self;
    // 根据条件发送查询（这里模拟搜索）
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // 搜素完毕
        // 显示搜索结果
        NSMutableArray *searchSuggestionsM = [NSMutableArray array];
        
        _searchSuggestionsM = searchSuggestionsM;
        searchViewController.searchSuggestions = searchSuggestionsM;
    });
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
